#include <QDebug>
#include "gamedatamodel.h"
#include "card.h"

SymbolType getRandomSymbol(uint quantaVariants){
	uint n = (uint)rand() % quantaVariants;
	return QString::number(n);
}

void GameDataModel::createCards(uint quantaCards, uint quantaVariants){
	m_cards.reserve(quantaCards);
	for(int i = 0; i < quantaCards / 2; ++i){
		SymbolType symbol = getRandomSymbol(quantaVariants);
		m_cards.append(new Card(symbol, this));
		m_cards.append(new Card(symbol, this));
	}
}

void GameDataModel::confuseCards(){
	int size = m_cards.size() - 1;
	for(int i = 0; i <= size; ++i)
		m_cards.move(i, rand() % size);
}

GameDataModel::GameDataModel(uint quantaCards, uint quantaVariants, QObject *parent) :
  QObject(parent)
{
	if(quantaCards % 2)
		--quantaCards;
	if(quantaVariants > quantaCards / 2)
		quantaVariants = quantaCards / 2;
	
	createCards(quantaCards, quantaVariants);
	qDebug()<<m_cards.size();
	confuseCards();
	qDebug()<<m_cards.size();
}

const QList<QObject*> &GameDataModel::cardsQML() const{
	return *((QList<QObject*>*)(&m_cards));
}

const QList<Card*> &GameDataModel::cards() const{
	return m_cards;
}
